package com.szosen.web;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.text.TextUtils;

import com.szosen.application.MyApplication;
import com.szosen.util.BackGroundThread;
import com.szosen.util.SharedpreferenceUtil;

public class Fetch {
	private static String NAMESPACE = "http://ws.hzehr.bsoft.com";// WSDL// 網址
	private static String SOAP_ACTION = "http://ws.ie.sinosoft.com/"; // SOAPAction

	private ResultListener listener;
	// 103.44.145.247
	static String mUrl = "http://1d569095i7.51mypc.cn:18067/CZEHR/services/DeviceImportService?wsdl";
	static String mUrl_Nei = "http://10.10.10.20:8090/CZEHR/services/DeviceImportService?wsdl";
	public static final String METHOD = "execute";
	/** 获取的用户数据的长度 */
	static String info = "<uploadinfo info=\"EHR_HealthRecord\">  <base_info alias=\"个人基本信息表\">     <idCard>440520195412022343</idCard><birthday>1954-12-02 00:00:00</birthday> <ywgms>030305</ywgms><ywgmsqt>其他药物过敏史</ywgmsqt>  </base_info>  <manageInfo>    <createUser>fxlxl</createUser>    <manageUnit>445103001</manageUnit>  </manageInfo></uploadinfo>";

	public static void communicate(final String xml, final String method,
			final ResultListener listener) {
		new BackGroundThread(new  BackGroundThread.BackGroundTask() {
			@Override
			public void process() {
				if (!MyApplication.isConnected()) {
					listener.failed("无网络访问");
					return;
				}
				SoapObject request = new SoapObject(NAMESPACE, method);
				// 傳遞參數
				request.addProperty("xmlParams", xml);
				System.out.println("请求" + xml);
				System.out.println("请求" + method);
				// request.addProperty("arg1", account.getText().toString());
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
						SoapEnvelope.VER11);
				envelope.bodyOut = request;
				String url = mUrl_Nei;
				String use_web = SharedpreferenceUtil.getUseWeb(
						MyApplication.getInstance(),
						SharedpreferenceUtil.USE_WEB_NEIWANG);
				if (!TextUtils.isEmpty(use_web)) {
					if (use_web.equals(SharedpreferenceUtil.USE_WEB_NEIWANG)) {
						url = mUrl_Nei;
					} else if (use_web
							.equals(SharedpreferenceUtil.USE_WEB_WAIWANG)) {
						url = mUrl;
					}
				}
				HttpTransportSE androidHttpTransport = new HttpTransportSE(url,
						0);
				try {
					androidHttpTransport.call(SOAP_ACTION + method, envelope);
					if (envelope.getResponse() != null) {
						// 獲取SOAP消息
						Object resObject = envelope.getResponse();
						listener.success(METHOD, resObject.toString());
						System.out.println(resObject);
					} else {
						listener.failed("服务器异常");
					}
				} catch (Exception e) {
					System.out.println("访问异常");
					e.printStackTrace();
					if (listener != null) {
						listener.failed("服务器异常");
					}
				}
			}
		}).start();

	}

	public static void main(String[] args) {
		communicate(info, "execute", new ResultListener() {

			public void success(String method, String msg) {

			}

			public void failed(String result) {

			}
		});
	}
}
